package com.uchiha.nova.mainService.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uchiha.nova.core.model.dto.TenantCompanyDTO;
import com.uchiha.nova.core.model.entity.TenantCompanyEntity;
import com.uchiha.nova.core.model.query.TenantCompanyQuery;
import com.uchiha.nova.core.model.vo.TenantCompanyVO;
import com.uchiha.nova.core.tool.api.R;
import com.uchiha.nova.core.tool.constant.NovaCRUDMethodConstant;
import com.uchiha.nova.core.tool.support.BaseController;
import com.uchiha.nova.core.tool.support.Condition;
import com.uchiha.nova.core.tool.support.DictBizVO;
import com.uchiha.nova.core.tool.support.QueryContext;
import com.uchiha.nova.core.tool.util.CascaderUtil;
import com.uchiha.nova.core.tool.util.Func;
import com.uchiha.nova.mainService.service.ITenantCompanyService;
import com.uchiha.nova.mainService.wrapper.TenantCompanyWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 公司表 控制器
 *
 * @author Uchihashiamu
 */
@RestController
@AllArgsConstructor
@RequestMapping("/tenantCompany")
@Tag(name = "公司表")
@Slf4j
public class TenantCompanyController extends BaseController {

    @Resource
    ITenantCompanyService tenantCompanyService;

    /**
     * 详情
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_WEB_GETONE)
    @Operation(summary = "详情")
    public R<TenantCompanyVO> getOne(@RequestBody TenantCompanyDTO tenantCompanyDTO) {
        TenantCompanyEntity entity = new TenantCompanyEntity();
        BeanUtil.copyProperties(tenantCompanyDTO, entity);
        TenantCompanyEntity detail = tenantCompanyService.getOne(Condition.getQueryWrapper(entity));
        return R.data(TenantCompanyWrapper.build().entityVO(detail));
    }

    /**
     * 分页 代码自定义代号
     */
    @GetMapping(NovaCRUDMethodConstant.NOVA_WEB_PAGE)
    @Operation(summary = "分页查询")
    public R getPage(TenantCompanyQuery query, QueryContext queryContext) {
        IPage<TenantCompanyEntity> pages = tenantCompanyService.page(Condition.getPage(queryContext), Condition.getQueryWrapper(query, queryContext, TenantCompanyEntity.class));
        return R.data(TenantCompanyWrapper.build().pageVO(pages));
    }

    /**
     * 新增 代码自定义代号
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_WEB_SAVE)
    @Operation(summary = "新增")
    public R save(@RequestBody TenantCompanyDTO tenantCompanyDTO) {
        TenantCompanyEntity entity = new TenantCompanyEntity();
        BeanUtil.copyProperties(tenantCompanyDTO, entity);
        if(ObjectUtil.isEmpty(entity.getLevel())){
            entity.setLevel(1);
        }
        if(ObjectUtil.isEmpty(entity.getParentId())){
            entity.setParentId(0L);
        }
        return R.status(tenantCompanyService.save(entity));
    }

    /**
     * 修改 代码自定义代号
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_WEB_UPDATE)
    @Operation(summary = "修改")
    public R update(@RequestBody TenantCompanyDTO tenantCompanyDTO) {
        TenantCompanyEntity entity = new TenantCompanyEntity();
        BeanUtil.copyProperties(tenantCompanyDTO, entity);
        return R.status(tenantCompanyService.updateById(entity));
    }

    /**
     * 新增或修改 代码自定义代号
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_WEB_SAVEORUPDATE)
    @Operation(summary = "新增或修改")
    public R saveOrUpdate(@RequestBody TenantCompanyDTO tenantCompanyDTO) {
        TenantCompanyEntity entity = new TenantCompanyEntity();
        BeanUtil.copyProperties(tenantCompanyDTO, entity);
        return R.status(tenantCompanyService.saveOrUpdate(entity));
    }


    /**
     * 删除 代码自定义代号
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_WEB_REMOVE)
    @Operation(summary = "逻辑删除")
    public R remove(@RequestParam String ids) {
        return R.status(tenantCompanyService.deleteLogic(Func.toLongList(ids)));
    }

    /**
     * 物理删除，不可恢复
     */
    @Operation(summary = "物理删除")
    @DeleteMapping(NovaCRUDMethodConstant.NOVA_WEB_DELETE)
    public R delete(@RequestParam String ids) {
        return R.status(tenantCompanyService.deletePhysics(Func.toLongList(ids)));
    }

    @GetMapping("/getTree")
    public R getTree() {
        List<TenantCompanyEntity> list = tenantCompanyService.list();
        List<DictBizVO> all = list.stream().map(item -> {
            DictBizVO dictBizVO = new DictBizVO();
            dictBizVO.setId(item.getId());
            dictBizVO.setParentId(item.getParentId());
            dictBizVO.setLabel(item.getName());
            dictBizVO.setValue(item.getId().toString());
            return dictBizVO;
        }).collect(Collectors.toList());
        List<DictBizVO> listLevelTop = all.stream().filter(item ->item.getParentId().compareTo(0L)==0).map(item -> {
            item.setChildren(CascaderUtil.getChildren(item, all));
            return item;
        }).collect(Collectors.toList());
        return R.data(listLevelTop);
    }
}
